home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 29 (1992-07)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].zip
/
MegaDisc 29 (1992-07)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].adf
/
Programming
/
Basic_Tutorial_10
/
Hex-bin-hex.bas
< prev
next >
Wrap
BASIC Source File
|
1992-08-01
|
4KB
|
134 lines
REM $OPTION Y+,FDH0:Progs/hex-bin-hex
SCREEN 1,640,256,2,2
WINDOW 1,,,,1
REM These are the two SHARED variables for the SUB Programs and they
REM are needed so that the converted numbers can be used in the main program.
BinNum$="":HexNum$=""
REM**************************************************************************
REM This is the main program.
Main:
CLS
LOCATE 10,15:PRINT "What would you like to do."
LOCATE 12,17:COLOR 1,0
PRINT "(1) ";:COLOR 3,0:PRINT "Convert from Hex to Binary."
LOCATE 13,17:COLOR 1,0
PRINT "(2) ";:COLOR 3,0:PRINT "Convert from Binary to Hex."
LOCATE 14,17:COLOR 1,0
PRINT "(3) ";:COLOR 3,0:PRINT "HELP."
LOCATE 15,17:COLOR 1,0
PRINT "(4) ";:COLOR 3,0:PRINT "Finished Thank You."
LOCATE 17,15:PRINT "Select a number ";:COLOR 1,0:PRINT "1,2 3 or 4 "
Aloop:
K$=INKEY$
IF K$ <"1" OR K$>"4" THEN Aloop
IF K$ ="1" THEN GOTO HexConvert
IF K$ ="2" THEN GOTO BinConvert
IF K$ ="3" THEN GOTO Help
IF K$ ="4" THEN SYSTEM
GOTO Aloop
HexConvert:
CLS:LOCATE 10,25
COLOR 3,0
PRINT "Enter a HEX number ";
COLOR 1,0
INPUT hexa$
PRINT
hexa$=UCASE$(hexa$)
HEXBIN(hexa$)
LOCATE 12,20
PRINT "The Binary Equivalent of ";:COLOR 3,0:PRINT "&H";hexa$;:COLOR 1,0:PRINT " is ";:COLOR 3,0:PRINT "&B";BinNum$
LOCATE 22,20:COLOR 1,0:PRINT "Would you like another [Y]es or [N]"
Dloop:
K$=INKEY$
IF K$="" THEN Dloop
IF K$="Y" OR K$="y" THEN HexConvert
IF K$="N" OR K$="n" THEN Main
GOTO Dloop
BinConvert:
CLS:PRINT:PRINT
COLOR 3,0:LOCATE 10,10
PRINT "Enter a binary number (i.e '0100' '11101111') ";
COLOR 1,0
INPUT bina$
BINHEX(bina$)
LOCATE 12,15
PRINT "The Hexadecimal Equivalent of ";:COLOR 3,0:PRINT "&b";bina$;:COLOR 1,0:PRINT " is ";:COLOR 3,0:PRINT "&H"HexNum$
LOCATE 22,20:COLOR 1,0:PRINT "Would you like another [Y]es or [N]"
Bloop:
K$=INKEY$
IF K$="" THEN Bloop
IF K$="Y" OR K$="y" THEN BinConvert
IF K$="N" OR K$="n" THEN Main
GOTO Bloop
Help:
CLS:PRINT
PRINT "A Hexadecimal number can be any of the following numbers or letters."
PRINT "0 1 2 3 4 5 6 7 8 9 A B C D E or F"
PRINT "Representing the numbers from 0 to 15"
PRINT "A Hexadecimal number is preceeded by the characters '&H'"
PRINT "So &HFF is equal to 255 in decimal notation"
PRINT
PRINT "Each of the Hexadecimal numbers is represented in Binary by a group of four "
PRINT "characters consisting of '0' or '1' as follows:-"
PRINT
PRINT " DECIMAL HEX BINARY DECIMAL HEX BINARY"
PRINT " 0 0 0000 8 8 1000"
PRINT " 1 1 0001 9 9 1001"
PRINT " 2 2 0010 10 A 1010"
PRINT " 3 3 0011 11 B 1011"
PRINT " 4 4 0100 12 C 1100"
PRINT " 5 5 0101 13 D 1101"
PRINT " 6 6 0110 14 E 1110"
PRINT " 7 7 0111 15 F 1111"
LOCATE 21,15:INPUT "Press RETURN for Menu ",dum$
GOTO Main
GOTO Main
REM**************** End of Main Program ************************************
REM Converts a Hexadecimal Number to a Binary Number
SUB HEXBIN (hexa$) STATIC
SHARED BinNum$
l=LEN(hexa$)
BinNum$=""
FOR i=1 TO l
RESTORE HexData
FOR x= 1 TO 16
READ h$,b$
IF MID$(hexa$,i,1)=h$ THEN BinNum$=BinNum$+ b$
NEXT x
NEXT i
HexData:
DATA "0","0000","1","0001","2","0010","3","0011","4","0100","5","0101","6","0110","7","0111"
DATA "8","1000","9","1001","A","1010","B","1011","C","1100","D","1101","E","1110","F","1111"
END SUB
REM Converts a Binary Number to a Hexadecimal Number
SUB BINHEX (bina$) STATIC
SHARED HexNum$
l=LEN(bina$)
HexNum$=""
FOR i=1 TO l STEP 4
RESTORE BinData
FOR x = 1 TO 16
READ h$,b$
IF MID$(bina$,i,4)=b$ THEN HexNum$=HexNum$+h$
NEXT x
NEXT i
BinData:
DATA "0","0000","1","0001","2","0010","3","0011","4","0100","5","0101","6","0110","7","0111"
DATA "8","1000","9","1001","A","1010","B","1011","C","1100","D","1101","E","1110","F","1111"
END SUB